import * as CryptoJS from 'crypto-ts';

const CRYPTOJSKEY = 'Movebroad135246A';
const iv = '0000000000000000';

export class AesManager {
  /*
   * {param} plaintText 加密明文
   * return  str 加密结果
   */
  public static encrypt(plaintText: string): string {
    const options = {
      iv: CryptoJS.enc.Utf8.parse(iv),
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.PKCS7,
    };
    const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
    console.log('key', key);
    const encryptedData = CryptoJS.AES.encrypt(plaintText, key, options);
    const encryptedBase64Str = encryptedData.toString();
    return encryptedBase64Str;
  }
  /*
   * {param} plaintText 解密密文

   * return  str 解密结果
   */
  public static decrypt(encryptedBase64Str: string): string {
    const options = {
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.PKCS7,
    };
    const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
    // 解密
    const decryptedData = CryptoJS.AES.decrypt(encryptedBase64Str, key, options);
    // 解密后，需要按照Utf8的方式将明文转位字符串
    const decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
    return decryptedStr;
  }
}
